# -*- coding: utf-8 -*-
# @Time    : 2019/11/8 14:35
# @Author  : Jinwei
# @File    : log.py

import os,logbook
from logbook.more import ColorizedStderrHandler
import traceback

check_path = '.'
LOG_DIR = os.path.join(check_path,'testlog')  #'./testlog'
file_stream = False

if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)
    file_stream = True

def error_cat(func):
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except:
            LOG.info("出错的函数名：{name}\n报错信息：{error}".format(name=func.__name__,error=traceback.format_exc()))
    return wrapper


def get_logger(name="Appium",file_log=file_stream,level=""):
    logbook.set_datetime_format('local')
    ColorizedStderrHandler(bubble=False,level=level).push_thread()
    logbook.TimedRotatingFileHandler(os.path.join(LOG_DIR,'%s.log' % name),date_format='%Y-%m-%d-%H',bubble=True,encoding='utf-8')
    return logbook.Logger(name)


LOG = get_logger(file_log=file_stream,level='INFO')

